﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using iTextSharp.text.pdf;
//using iTextSharp.text;
//using System.IO;
//using sharpPDF;
//using sharpPDF.Fonts;

//namespace Check_Printing_Solution
//{
//    public class ChequePDFGenerator
//    {
//        PrintManager manager = new PrintManager();

//        private TemplateLayout Template { get; set; }
//        private Customer CurrentCustomer { get; set; }
//        private Bank CurrentBank { get; set; }
//        private string CoverpageNextChequeNumber { get; set; }
//        private string LastChequeNumber { get; set; }
//        private int Incrementer { get; set; }
//        private int PageCount { get; set; }
//        private int TotalChequesPrinted { get; set; }
//        private int ChequesPrinted { get; set; }
//        private int ChequeCount { get; set; }
//        private string CoversFilePath { get; set; }
//        private string TempCoversFilePath { get; set; }
//        private pdfDocument CoversPDFDocument { get; set; }

//        public void InitiatePDFGenerationProcess( Customer customer, Bank bank, string style, int totalchequesforprint,
//            string comment, string nextchequenumber, string styleid )
//        {
//            int pagesperbook = 0;
//            int numbooks = 0;
//            int remainder = 0;

//            CurrentCustomer = customer;
//            CurrentBank = bank;
//            CoverpageNextChequeNumber = ( Convert.ToInt32( nextchequenumber ) + totalchequesforprint ).ToString();
//            LastChequeNumber = nextchequenumber;
//            CurrentBank = manager.GetBankByBankCode( bank.BranchPrefix );
//            Template = manager.GetTemplateLayout( style, styleid );
//            pagesperbook = Convert.ToInt32( Template.NoChequesPerBook );

//            TempCoversFilePath = System.Configuration.ConfigurationSettings.AppSettings["PDFTempPath"].ToString() + customer.ChequeNumber + "_Covers.pdf";
//            CoversFilePath = System.Configuration.ConfigurationSettings.AppSettings["PDFPath"].ToString() + customer.ChequeNumber + "_Covers.pdf";
            
//            if ( totalchequesforprint > pagesperbook )
//            {
//                numbooks = totalchequesforprint / Convert.ToInt32( Template.ChquesPerPage );
//                numbooks = numbooks / Convert.ToInt32( Template.NoChequesPerBook );
//                remainder = totalchequesforprint - ( numbooks * Convert.ToInt32( Template.NoChequesPerBook ) * Convert.ToInt32( Template.ChquesPerPage ) );
//            }
//            else if ( pagesperbook > totalchequesforprint )
//            {
//                remainder = totalchequesforprint;
//            }
//            else
//            {
//                numbooks = 1;
//            }

//            Incrementer = Convert.ToInt32( Template.NoChequesPerBook );

//            for ( int j = 0; j < numbooks; j++ )
//            {
//                if ( Template.PrintCovers )
//                {
//                    GenerateCoverPDFs();
//                }
//                if ( PageCount != 1 && !Template.Skip )
//                {
//                    PageCount = TotalChequesPrinted + 1;
//                }
//                int pages = totalchequesforprint / Convert.ToInt32( Template.ChquesPerPage );
//                GenerateChequePDFs( Template, pages );
//                ChequesPrinted = 0;
//                ChequeCount = 1;
//            }

//            if ( remainder > 0 )
//            {
//                if ( totalchequesforprint < pagesperbook )
//                {
//                    Incrementer = totalchequesforprint / Convert.ToInt32( Template.ChquesPerPage );
//                }
//                if ( Template.PrintCovers )
//                {
//                    GenerateCoverPDFs();
//                }
//                if ( PageCount != 1 && !Template.Skip )
//                {
//                    PageCount = TotalChequesPrinted + 1;
//                }
//                GenerateChequePDFs( Template, remainder );
//                ChequesPrinted = 0;
//                ChequeCount = 1;
//            }

//            CreateCoversPDFFile();

//            CreateJobLog( comment,
//                totalchequesforprint.ToString(),
//                "0",
//                customer.ChequeNumber,
//                style
//                );
//        }

//        private void SetupCoversPDF()
//        {
//            //pdfDocument myDoc = new pdfDocument( "AgileLight - Cheque Printing Solution - Kendell Patrice",
//            //    "AgileLight - Cheque Printing Solution, Kendell Patrice, kendell.patrice@gmail.com" );

//            //Initialize Document
//            CoversPDFDocument = new pdfDocument( "AgileLight - Cheque Printing Solution - Kendell Patrice",
//                "AgileLight - Cheque Printing Solution, Kendell Patrice, kendell.patrice@gmail.com" );

//            //Setup Font
//            CoversPDFDocument.addTrueTypeFont( @"C:\MICR\MICR 010 BT.ttf", "MICR 010 BT" );
//        }

//        private void CreateCoversPDFFile()
//        {
//            CoversPDFDocument.createPDF( @"C:\MICR\temp\" + TempCoversFilePath );
//            CoversPDFDocument = null;
//        }

//        private void GenerateCoverPDFs()
//        {
//            ChequesPrinted = 0;

//            if ( Template.Landscape )
//            {
//                //template.DefaultPageSettings.Landscape = true;
//            }
//            else
//            {
//                //template.DefaultPageSettings.Landscape = false;
//            }

//            int padding = 0;

//            for ( int i = 0; i < Convert.ToInt32( Template.ChquesPerPage ); i++ )
//            {
//                pdfAbstractFont font = CoversPDFDocument.getFontReference( "MICR 010 BT" );

//                if ( Template.BoldNameCover == true )
//                {
//                    //fntstyle = FontStyle.Bold;
//                }
//                else
//                {
//                    //fntstyle = FontStyle.Regular;
//                }

//                //e.Graphics.DrawString( CurrentBank.BankAddress, new Font( Template.CoverAddress4Font, Convert.ToSingle( Template.CoverAddress4Size ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverAddress4X ), Convert.ToInt32( _template.CoverAddress4Y ) + padding ) );
//                ////Name
//                //e.Graphics.DrawString( _currentcustomer.FirstName + " " + _currentcustomer.LastName, new Font( Template.CoverNameFont, Convert.ToSingle( _template.CoverNameSize ), FontStyle.Bold ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverNameX ), Convert.ToInt32( _template.CoverNameY ) + padding ) );
//                ////Address1
//                //e.Graphics.DrawString( _currentcustomer.Line2, new Font( Template.CoverAddress1Font, Convert.ToSingle( Template.CoverAddress1Size ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverAddress1X ), Convert.ToInt32( _template.CoverAddress1Y ) + padding ) );
//                ////Address2
//                //e.Graphics.DrawString( _currentcustomer.Line3, new Font( Template.CoverAddress2Font, Convert.ToSingle( Template.CoverAddress2Size ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverAddress2X ), Convert.ToInt32( _template.CoverAddress2Y ) + padding ) );
//                ////Phone
//                //e.Graphics.DrawString( _currentcustomer.Line1, new Font( Template.CoverTelephoneFont, Convert.ToSingle( Template.CoverTelephoneSize ), fntstyle ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverTelephoneX ), Convert.ToInt32( _template.CoverTelephoneY ) + padding ) );
//                ////Next Cheque #
//                //e.Graphics.DrawString( FormatSeqNumber( _coverpage_nextchequenumber ), new Font( _template.CoverCheckCountFont, Convert.ToSingle( _template.CoverCheckCountSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverCheckCountX ), Convert.ToInt32( _template.CoverCheckCountY ) + padding ) );
//                ////CoverCheckCount

//                //string checkno = string.Format( "C{0}C A{1}D{2}A {3}C  {4}", FormatSeqNumber( _coverpage_nextchequenumber ), _currentbank.BranchPrefix, _currentbank.BranchCode, _currentcustomer.ChequeNumber, _template.DepositSlips );
//                //e.Graphics.DrawString( checkno, new Font( Template.CoverCheckNoFont, Convert.ToSingle( Template.CoverCheckNoSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverCheckNoX ), Convert.ToInt32( _template.CoverCheckNoY ) + padding ) );

//                //padding = padding + Convert.ToInt32( Template.DistanceBetweenCheques );
//                //_coversprinted++;
//            }

            
            
            
//            //pdfPage myPage = CoversPDFDocument.addPage();
//            //myPage.addText( "C0003C A02890D003A 00090012345678C  46", 100, 450, font, 19 );

//            //pdfPage myPage2 = CoversPDFDocument.addPage();
//            //myPage2.addText( "C0003C A02890D003A 00090012345678C  47", 100, 450, font, 19 );



//            //string newfile = "finished-" + Guid.NewGuid().ToString() + ".pdf";
//            ////Read old file
//            //PdfReader reader = new PdfReader( @"C:\MICR\temp\" + oldfile );
//            //iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation( 1 );
//            //Document document = new Document( size );
//            //FileStream fs = new FileStream( @"C:\MICR\" + newfile, FileMode.Create );
//            //PdfWriter writer = PdfWriter.GetInstance( document, fs );

//            ////Add meta information to the document
//            //document.AddAuthor( "AgileLight - Cheque Printing Solution, Kendell Patrice, kendell.patrice@gmail.com" );
//            //document.AddCreator( "AgileLight" );
//            //document.AddKeywords( "AgileLight, Cheque Printing Solution, Kendell Patrice" );
//            //document.AddSubject( "AgileLight c/o DATA FORMS LTD, Cheques Printed for Processing" );
//            //document.AddTitle( "AgileLight - Cheque Printing Solution - Kendell Patrice" );

//            //// Open the document to enable you to write to the document
//            //document.Open();

//            //BaseFont bf = BaseFont.CreateFont( BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED );

//            //for ( int i = 0; i < reader.NumberOfPages; i++ )
//            //{
//            //    PdfContentByte cb = writer.DirectContent;

//            //    document.NewPage();
//            //    cb.SetColorFill( BaseColor.BLACK );
//            //    cb.SetFontAndSize( bf, 12 );

//            //    // write the text in the pdf content
//            //    cb.BeginText();
//            //    string text = "Some random blablablabla...";

//            //    // put the alignment and coordinates here
//            //    cb.ShowTextAligned( 1, text, 520, 640, 0 );
//            //    cb.EndText();
//            //    cb.BeginText();
//            //    text = "Other random blabla...";

//            //    // put the alignment and coordinates here
//            //    cb.ShowTextAligned( 2, text, 100, 200, 0 );
//            //    cb.EndText();

//            //    // create the new page and add it to the pdf
//            //    PdfImportedPage page = writer.GetImportedPage( reader, i + 1 );
//            //    cb.AddTemplate( page, 0, 0 );
//            //}

//            //// Close the document
//            //document.Close();
//            //// Close the writer instance
//            //writer.Close();
//            //// Always close open filehandles explicity
//            //fs.Close();
//        }

//        private void GenerateChequePDFs( TemplateLayout template, int pages )
//        {
//            string oldfile = "helper-" + Guid.NewGuid().ToString() + ".pdf";
//            string newfile = "finished-" + Guid.NewGuid().ToString() + ".pdf";

//            pdfDocument myDoc = new pdfDocument( "AgileLight - Cheque Printing Solution - Kendell Patrice",
//                "AgileLight - Cheque Printing Solution, Kendell Patrice, kendell.patrice@gmail.com" );

//            myDoc.addTrueTypeFont( @"C:\MICR\MICR 010 BT.ttf", "MICR 010 BT" );
//            pdfAbstractFont font = myDoc.getFontReference( "MICR 010 BT" );

//            pdfPage myPage = myDoc.addPage();
//            myPage.addText( "C0003C A02890D003A 00090012345678C  46", 100, 450, font, 19 );

//            pdfPage myPage2 = myDoc.addPage();
//            myPage2.addText( "C0003C A02890D003A 00090012345678C  47", 100, 450, font, 19 );

//            myDoc.createPDF( @"C:\MICR\temp\" + oldfile );
//            myPage = null;
//            myDoc = null;

//            //Read old file
//            PdfReader reader = new PdfReader( @"C:\MICR\temp\" + oldfile );
//            iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation( 1 );
//            Document document = new Document( size );
//            FileStream fs = new FileStream( @"C:\MICR\" + newfile, FileMode.Create );
//            PdfWriter writer = PdfWriter.GetInstance( document, fs );

//            //Add meta information to the document
//            document = AddMetaData( document );

//            // Open the document to enable you to write to the document
//            document.Open();

//            //Create font
//            BaseFont bf = BaseFont.CreateFont( BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED );

//            for ( int i = 0; i < reader.NumberOfPages; i++ )
//            {
//                PdfContentByte cb = writer.DirectContent;

//                document.NewPage();
//                cb.SetColorFill( BaseColor.BLACK );
//                cb.SetFontAndSize( bf, 12 );

//                // write the text in the pdf content
//                cb.BeginText();
//                string text = "Some random blablablabla...";

//                // put the alignment and coordinates here
//                cb.ShowTextAligned( 1, text, 520, 640, 0 );
//                cb.EndText();
//                cb.BeginText();
//                text = "Other random blabla...";

//                // put the alignment and coordinates here
//                cb.ShowTextAligned( 2, text, 100, 200, 0 );
//                cb.EndText();

//                // create the new page and add it to the pdf
//                PdfImportedPage page = writer.GetImportedPage( reader, i + 1 );
//                cb.AddTemplate( page, 0, 0 );
//            }

//            // Close the document
//            document.Close();
//            // Close the writer instance
//            writer.Close();
//            // Always close open filehandles explicity
//            fs.Close();
//        }

//        private string FormatSeqNumber( string seqnumber )
//        {
//            int templaterequired = Convert.ToInt32( Template.SeqNumberSize );
//            int currentlength = seqnumber.ToString().Count();
//            int gap = templaterequired - currentlength;
//            string holder = "";

//            for ( int i = 0; i < gap; i++ )
//            {
//                holder = holder + "0";
//            }
//            holder = holder + seqnumber;

//            return holder;
//        }

//        private void CreateJobLog( string comment, string quantity, string lastchequenumber, string accountnumber, string chequetype )
//        {
//            JobLog joblog = new JobLog();
//            joblog.Comment = comment;
//            joblog.DatePrinted = DateTime.Now.ToString();
//            joblog.LastQuantity = quantity;
//            joblog.LastChequeNumber = lastchequenumber;
//            joblog.CustomerId = accountnumber;
//            joblog.ChequeTypeId = chequetype;

//            manager.AddJobLog( joblog );
//        }

//        private Document AddMetaData( Document document )
//        {
//            document.AddAuthor( "AgileLight - Cheque Printing Solution, Kendell Patrice, kendell.patrice@gmail.com" );
//            document.AddCreator( "AgileLight" );
//            document.AddKeywords( "AgileLight, Cheque Printing Solution, Kendell Patrice" );
//            document.AddSubject( "AgileLight c/o DATA FORMS LTD, Cheques Printed for Processing" );
//            document.AddTitle( "AgileLight - Cheque Printing Solution - Kendell Patrice" );

//            return document;
//        }

//        private void CleanUp( string filename )
//        {
//            File.Delete( filename );
//        }
//    }
//}
